package socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class  Client {
    private Socket socket;

    public Client(){

        try {
            System.out.println("正在连接服务端");
            //socket = new Socket("176.201.104.15",8088);//hpr
            socket = new Socket("127.0.0.1",8088);
            //socket = new Socket("176.201.104.25",8088);//wg
            //socket = new Socket("176.201.104.9",8088);//llx
            System.out.println("连接成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  void start(){
        Scanner scan = new Scanner(System.in);
        String say = scan.nextLine();
            try {
                OutputStream out = socket.getOutputStream();
                //out.write(1);
                while(!"exit".equalsIgnoreCase( say)) {
                byte[] data = say.getBytes(StandardCharsets.UTF_8);
                out.write(data.length);
                out.write(data);
                say = scan.nextLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    socket.close();
                    System.out.println("连接已断开");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
